Showing posts with label Console. Show all posts
Showing posts with label Console. Show all posts

How To Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.

Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.



  1. //Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.
  2. #include<string.h>
  3. #include<iostream>
  4. using namespace std;
  5. int main()
  6. {
  7.     char c1[20],c2[20];
  8.     int i;
  9.     cin>>c1;
  10.     cin>>c2;
  11.     for(i=0;i<strlen(c1);i++)
  12.         if(c1[i]>='a'&&c1[i]<='z')
  13.             c1[i]=c1[i]-32;
  14.         else
  15.             if(c1[i]>='A'&&c1[i]<='Z')
  16.                 c1[i]=c1[i]+32;
  17.     for(i=0;i<strlen(c2);i++)
  18.         if(c2[i]>='a'&&c2[i]<='z')
  19.             c2[i]=c2[i]-32;
  20.         else
  21.             if(c2[i]>='A'&&c2[i]<='Z')
  22.                 c2[i]=c2[i]+32;
  23.         cout<<c1<<" "<<c2;
  24. }

C Program to Defines the entry point for the console application

How to write a C Program to Defines the entry point for the console application in C Programming Language ?


Solution:


//Defines the entry point for the console application 
#include "stdio.h"


int main(int argc, char argv[])
{
  unsigned int x,y,p,n,ergebnis;
 printf("Bitte gegen sie die Zahl ein die Veraendert werden soll: ");
 scanf("%d", &x);
 printf("\nBitte geben sie die Zahl ein von der die zu Veraendernde Bits kommen sollen:");
 scanf("%d", &y);
 printf("\nBitte geben sie die Position der Bits ein von der an die Veraenderung stattfinden soll (Zaehlung beginnt rechts mit Bit 0): ");
 scanf("%d", &p);
 printf("\nBitte geben sie die Anzahl der Bits ein die veraendert werden sollen: ");
 scanf("%d", &n);
 ergebnis= ~y;
 ergebnis= ergebnis << (32-n);
 ergebnis= ergebnis >> (32-(p+1));
 ergebnis= ergebnis ^ x;
 ergebnis= ergebnis & x;
 printf("\n%d\n",ergebnis);
 
 char ch;
 scanf("%c",& ch);
 getchar();
 return 0;
}

C Program to Defines the entry point for the console application

How to write a C Program to Defines the entry point for the console application in C Programming Language ?


Solution:
/*Defines the entry point for the console application*/
//Defines the entry point for the console application.

Defines the entry point for the console application.

How to write a C program to Defines the entry point for the console application in C Programming Language ?

Solution:
//Defines the entry point for the console application.
//

#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS

float somatorio(int N) {

float S = 0, termo=1;
for (int i = 1; i <= N; i++)
{
termo = termo * (2.0f / i);
S += termo;
}
return S;
}
//exercicio 6
float somatorio6(int N)
{
float soma = 1;
int sinal = -1;
for (int i = 1; i <= N; i++)
{
//soma = soma + 1.0f / (2 * i); //solucao com +

if (i % 2 == 0)
soma += 1 / (i * 2.0f);
else
soma -= 1 / (i * 2.0f);
/*
//Solucao 2
soma = soma + (1.0f / (2 * i))*sinal;
sinal = -sinal;*/
}
return soma;
}

//meia arvore de natal: Esquerda -> Direita
void meiaarvoreesquerda(int N)
{
for (int linha = 0; linha < N; linha++)
{
for (int ramo = 0; ramo <= linha; ramo++)
printf("%d",ramo+1);

printf("\n");
}

}
//Asterisco
void asterisco(int N)
{
for (int linha = 0; linha < N; linha++)
{
for (int ramo = 0; ramo <= linha; ramo++)
printf("*");

printf("\n");
}

}
//arvore direita para esquerda
//meia arvore de natal: direita -> esquerda
void meiaarvoredireita(int N)
{
for (int linha = 1; linha <= N; linha++)
{
for (int k = 0; k < N - linha; k++)
printf(" ");
for (int ramo = 0; ramo < linha; ramo++)
printf("*");

printf("\n");
}

}
//exercicio 10
float potencia(float a, int N)
{
float potencia = 1;
for (int i = 1; i <= N; i++)
potencia *= a;

return potencia;
}
//exercicio 9 : Capital Acumulado
float calculoCA(float CI, float j, int N)
{
return CI*potencia(1 + j, N);
}

//exercicio 11 B alterada
void mediaproduto()
{
int n = 0;
float media = 0.0, valor = 0.0, produto=1;

do {
printf("Valor:");
scanf("%f", &valor);
if (valor > 0) {
media += valor;
produto *= valor;
n++;
}
} while (valor > 0);
printf(" Media:%.3f produto:%.3f", media, produto);
printf("\n");
}
int main()
{
/*int N;
float j;
float CI;
printf("N:");
scanf("%d", &N);
printf("j:");
scanf("%f", &j);
printf("CI:");
scanf("%f", &CI);
//.3 = 3 casas decimais
printf("%.3f", calculoCA(CI,j,N));
printf("\n");
//meiaarvoredireita(N);
*/
mediaproduto();
return 0;
}

File Handling (console to file) in C Program

How to write a C Program Writing a character into a file,reading the character from the console and writing the next letter into the file in C Programming Language ?



Solution:
/* Writing a character into a file,reading the character from the console and writing the next letter into the file*/

#include<stdio.h>
#include<stdlib.h>

int main()
{

FILE *fp; //creates a file pointer
char ch;
int con;


fp=fopen("filehandling.txt","w+"); // creates a text file with label "filehandling" in write mode

if(fp==NULL)  //checks for the filename validity
{
printf("\nUnable to open the file\n");
exit(0);
}
printf("\nFile opened successfully\n");

printf("\nEnter the character as input\n");
scanf("%c",&ch);


putc(ch,fp);

fclose(fp);

fp=fopen("filehandling.txt","rwa+");

ch=getc(fp);


con=ch;
con++;
if(con>=97 && con<122)
{

fprintf(fp,"\nthe next char is %c\n",con);
}
else
{
fprintf(fp,"\nthe next char is a\n");
fclose(fp);

// open filehandling.txt file and check the input and output given...
}
}